﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Globalization;
using Pipra.Math.Geometry;

namespace Pipra.Gis
{
    /// <summary>
    /// A lat-lon coordinate with height.
    /// </summary>
    public struct LatLonH :
        ILatLonH<double>,
        IComparable<LatLonH>,
        IEquatable<LatLonH>
    {
        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">A coordinate.</param>
        /// <param name="b">A coordinate.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(LatLonH a, LatLonH b)
        {
            return a.Equals(b);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">A coordinate.</param>
        /// <param name="b">A coordinate.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(LatLonH a, LatLonH b)
        {
            return !a.Equals(b);
        }

        /// <summary>
        /// The latitude.
        /// </summary>
        public readonly double Lat;
        /// <summary>
        /// The longitude.
        /// </summary>
        public readonly double Lon;
        /// <summary>
        /// The height.
        /// </summary>
        public readonly double Height;
        /// <summary>
        /// Creates a new lat-lon point with height.
        /// </summary>
        /// <param name="lat">The latitude.</param>
        /// <param name="lon">The longitude.</param>
        /// <param name="h">The height.</param>
        public LatLonH(double lat, double lon, double h)
        {
            Lat = lat;
            Lon = lon;
            Height = h;
        }

        double ILatLonH<double>.Lat
        {
            get { return Lat; }
        }

        double ILatLonH<double>.Lon
        {
            get { return Lon; }
        }

        double ILatLonH<double>.Height
        {
            get { return Height; }
        }

        double ICoordinateTriple<double>.X
        {
            get { return Lon; }
        }

        double ICoordinateTriple<double>.Y
        {
            get { return Lat; }
        }

        double ICoordinateTriple<double>.Z
        {
            get { return Height; }
        }

        public int CompareTo(LatLonH other)
        {
            int c = Lon.CompareTo(other.Lon);
            if(0 == c)
            {
                c = Lat.CompareTo(other.Lat);
                return 0 == c ? Height.CompareTo(other.Height) : c;
            }
            return c;
        }

        public int CompareTo(ICoordinateTriple<double> other)
        {
            if (null == other)
            {
                return 1;
            }
            int c = Lon.CompareTo(other.X);
            if (0 == c)
            {
                c = Lat.CompareTo(other.Y);
                return 0 == c ? Height.CompareTo(other.Z) : c;
            }
            return c;
        }

        public bool Equals(LatLonH other)
        {
            return Lon == other.Lon
                && Lat == other.Lat
                && Height == other.Height
            ;
        }

        public bool Equals(ICoordinateTriple<double> other)
        {
            return !ReferenceEquals(null, other)
                && Lon == other.X
                && Lat == other.Y
                && Height == other.Z
            ;
        }

        public override string ToString()
        {
            return String.Format(CultureInfo.InvariantCulture, "{0},{1},{2}", Lat, Lon, Height);
        }

        public override int GetHashCode()
        {
            return Lon.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return null != obj
                && (
                    obj is LatLonH ? Equals((LatLonH)obj)
                    :
                    obj is ICoordinateTriple<double> && Equals(obj as ICoordinateTriple<double>)
                )
            ;

        }
    }
}
